package com.ktjy.crm.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.crm.entity.SysRight;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class RightMapperTester {
    @Resource
    private RightMapper rightMapper;

    //根据id查询
    @Test
    public void testSelectById() {
        SysRight sysRight = rightMapper.selectById("L01");
        System.out.println(sysRight);
    }

    //查询所有
    @Test
    public void testSelectAll() {
        List<SysRight> sysRights = rightMapper.selectList(null);
        System.out.println(sysRights);
    }

    //插入
    @Test
    public void testInsert() {
        int insert = rightMapper.insert(new SysRight("L015", "L01", "L01",
                "L01", "L01", "L01"));
        System.out.println(insert);
    }

    //更新
      @Test
    public void testUpdate() {
        // 创建要更新的实体对象
        SysRight sysRight = new SysRight("L015", "老六", "L01", "L01", "L01", "L01");
        // 创建更新条件包装器，这里以 id 为 L015 作为更新条件
        com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<SysRight> updateWrapper = new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<>();
        updateWrapper.eq("right_code", "L015");
        // 调用 update 方法，传入实体对象和更新条件包装器
        int update = rightMapper.update(sysRight, updateWrapper);
        System.out.println(update);
    }

    //删除
    @Test
    public void testDelete() {
        // 创建删除条件包装器，这里以 id 为 L015 作为删除条件
        QueryWrapper<SysRight> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("right_code", "L015");

        // 调用 delete 方法，传入删除条件包装器
        int delete = rightMapper.delete(queryWrapper);
        System.out.println(delete);
    }

    @Test
    public void testSelectByParentCode() {      //分页查询
        QueryWrapper<SysRight> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("right_parent_code", "L01");
        Page<SysRight> page = new Page<>(1, 5);
        IPage<SysRight> sysRights = rightMapper.selectPage(page, queryWrapper);
        System.out.println("总条数：" + sysRights.getTotal());
        System.out.println("总页数：" + sysRights.getPages());
        System.out.println("当前页：" + sysRights.getCurrent());
        System.out.println("每页显示数：" + sysRights.getSize());
        System.out.println("当前页数据：");
        for (SysRight sysRight : sysRights.getRecords()) {
            System.out.println(sysRight);
        }
    }

}
